package com.itheima.test04;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
定义一个线程类，实现Callable接口。此线程可以计算1--100的所有数字的累加和。
 */
public class MyCallable implements Callable<String> {
    private static int sum = 0;
    private int num;
    private static Object object = new Object();
    /*public MyCallable() {
    }
*/
    public MyCallable(int num) {
        this.num = num;
    }

    @Override
    public String call() throws Exception {

        synchronized (object) {
            sum += num;
            System.out.println(Thread.currentThread().getName() + ":sum = " + sum);
        }
        return Thread.currentThread().getName() + "添加成功";
    }
}
